﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using YouHP.Model;

namespace YouHP.Atom
{
    public class PatientPrescriptionHistories
    {
        // Get patient prescription histories by PrId only.
        static readonly string _sGetHistoriesByPrIdNoPaging = "GetPrescriptionHistoriesByPrIdNoPaging";
        // Get paged patient prescription histories by PrId only.
        static readonly string _sGetHistoriesByPrId = "GetPrescriptionHistoriesByPrId";
        // Get paged patient prescription histories by PrId and PrescriptionId.
        static readonly string _sGetHistoriesByPrescriptionId = "GetPrescriptionHistoriesByPrescriptionId";

        static readonly string _sGetPrescriptionByPrdId = "GetPrescriptionsByPrdId";

        // --------------------------------------------------------------------------------------
        // Get paged patient prescription histories by PrId ONLY!!
        // --------------------------------------------------------------------------------------
        public List<PrescriptionHistoryInfo> GetHistoriesByPrId(int prId, int iPageSize, int iPageNo, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
               _sGetHistoriesByPrId, prId, iPageSize, iPageNo);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<PrescriptionHistoryInfo>(dr);
        }


        // --------------------------------------------------------------------------------------
        // Get paged patient prescription histories by PrId and PrescriptionId!!
        // --------------------------------------------------------------------------------------
        public List<PrescriptionHistoryInfo> GetHistoriesByPrescriptionId(int prId, int iPrescriptionId, int iPageSize, int iPageNo, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr, 
                _sGetHistoriesByPrescriptionId, prId, iPrescriptionId, iPageSize, iPageNo);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<PrescriptionHistoryInfo>(dr);
        }

        // --------------------------------------------------------------------------------------
        // Get patient prescription histories by PrId ONLY!!
        // --------------------------------------------------------------------------------------
        public List<PrescriptionHistoryInfo> GetHistoriesByPrId(int prId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
                 _sGetHistoriesByPrIdNoPaging, prId);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            int iCount = 0;
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<PrescriptionHistoryInfo>(dr);
        }

        public List<PrescriptionHistoryInfo> GetPrescriptionByPrId(int prdId)
        {
            return CBO.FillCollection<PrescriptionHistoryInfo>(SqlHelper.ExecuteReader(Common.ConnStr, _sGetPrescriptionByPrdId, prdId));
        }
    }
}
